package api.image;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.StringTokenizer;

import api.components.ImagePanel;

public class MyImage {
	public static final int LEVELS = 256;
	public static final int ALPHA = (0xff << 24);
	public static final int RED = 0;
	public static final int GREEN = 1;
	public static final int BLUE = 2;
	
	private File file;
	public BufferedImage img;
	
	public BufferedImage imgMonocromo; // Tener la opcion de la transformacion pero no crearla dentro de esta imagen
	
	public ImagePanel panel;
	public boolean saved;
	public String name;
	public String extension;
	public int filas;
	public int cols;
	
	public int minGray = LEVELS;
	public int maxGray = -1;
	public double brillo = 0;
	public double contraste = 0;
	public double entropia = 0;
	
	public int[] histogram = new int[LEVELS]; 
	public int[] histogramAcum = new int[LEVELS];
	
	
	public MyImage(File file, BufferedImage img, boolean saved) {
		this.file = file;
		this.img = img;
		this.saved = saved;
		
		SepararNombre();
		
		this.filas = img.getWidth();
		this.cols = img.getHeight();
		
		//imgMonocromo = new BufferedImage(filas, cols, BufferedImage.TYPE_INT_RGB);
		//ConvertirFormatoPAL();
		
		obtenerParametros();
		
		this.panel = new ImagePanel(this);
	}
	
	
	public void obtenerParametros() {
		int[] pixel = new int[3];
		double grayDouble = 0;
		int gray = 0;
		
		for (int x = 0; x < filas; x++) {
			for (int y = 0; y < cols; y++) {
				readPixel(img.getRGB(x, y), pixel);
				
				grayDouble = (0.222 * pixel[RED] + 0.707 * pixel[GREEN] + 0.071 * pixel[BLUE]);
				gray = (int) Math.round(grayDouble);
				
				this.histogram[gray]++;
				
				minGray = (gray < minGray) ? gray : minGray;
				maxGray = (gray > maxGray) ? gray : maxGray;
				
				brillo += gray;
			}
		}
		
		brillo /= (filas * cols);
		
		histogramAcum[LEVELS - 1] = filas * cols;
		
		for (int i = histogram.length - 2; i >= 0; i--)
			histogramAcum[i] = histogramAcum[i + 1] - histogram[i + 1];
		
		for (int i = 0; i < histogram.length; i++) {
			contraste += Math.pow(i - brillo, 2) * histogram[i];
			
			entropia -= (histogram[i] / (double) (filas * cols) * 
					(Math.log10(histogram[i] / (double) (filas * cols)) / Math.log10(2)));
		}
		
		contraste = Math.sqrt((1 / (double) (filas * cols)) * contraste);
	}
	
	public void SepararNombre() {
		StringTokenizer st = new StringTokenizer(this.file.getName());
		
		this.name = st.nextToken(".");
		this.extension = st.nextToken();
	}
	
	public void ConvertirFormatoPAL() {
		int[] pixel = new int[3];
		
		for (int x = 0; x < filas; x++) {
			for (int y = 0; y < cols; y++) {
				readPixel(img.getRGB(x, y), pixel);
				
				// formato pal
				pixel[RED] = pixel[GREEN] = pixel[BLUE] = 
						(int) ((int) (0.222 * pixel[RED]) + (0.707 * pixel[GREEN]) + (0.071 * pixel[BLUE])); 
				
				imgMonocromo.setRGB(x, y, getPixel(pixel));
			}
		}				
	}
	
	// separa el valor de cada pixel y mete cada valor en la posicion del array
	public final void readPixel(int pixel, int[] color) {
		color[RED] = (pixel & 0x00ff0000) >> 16;
		color[GREEN] = (pixel & 0x0000ff00) >> 8;
		color[BLUE]	= (pixel & 0x000000ff);
	}
	
	public final int getPixel(int[] color) {
		return (ALPHA | (color[RED] << 16) | (color[GREEN] << 8) | color[BLUE]);
	}
	
}
